package com.server.logs.aspect;

import com.server.logs.annotation.LogRecord;
import com.server.logs.entity.SysLogs;
import com.server.logs.service.ISysLogsService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author zgr
 * @date 2023/7/11 14:45
 * @description
 */
@Slf4j
@Aspect
public class LogRecordAspect {

    private final ISysLogsService sysLogsService;

    public LogRecordAspect(ISysLogsService sysLogsService) {
        this.sysLogsService = sysLogsService;
    }

    @Pointcut("@annotation(com.server.logs.annotation.LogRecord)")
    public void logRecordAspect() {}

    @Around("logRecordAspect() && @annotation(logRecord)")
    public Object around(ProceedingJoinPoint joinPoint, LogRecord logRecord) throws Throwable {
        log.info("joinPoint:{}", joinPoint);
        //方法名
        String methodName = joinPoint.getSignature().getName();
        //类名
        String className = joinPoint.getTarget().getClass().getName();
        //执行方法
        Object o = joinPoint.proceed();

        SysLogs sysLogs = new SysLogs();
        sysLogs.setContent(logRecord.content());
//        sysLogs.setRemoteIp();
//        sysLogs.setEnv();
//        sysLogs.setParams();
//        sysLogs.setMethodClass();
//        sysLogs.setMethodName();
//        sysLogs.setTitle();
//        sysLogs.setRequestUri();
//        sysLogs.setServerIp();
//        sysLogs.setServerHost();

        return sysLogsService.save(sysLogs);
    }


}
